<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>设置详细地址Frame</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <style>
    html,
    body {
        height: 100%;
    }
    
    body {
        background-color: #eee;
    }
    
    header {
        box-sizing: border-box;
        display: -webkit-box;
        display: -webkit-flex;
        display: flex;
        -webkit-box-orient: horizontal;
        -webkit-flex-flow: row;
        flex-flow: row;
        padding: 16px;
        height: 60px;
        margin-bottom: 2px;
        background-color: #fff;
    }
    
    header .input {
        -webkit-box-flex: 1;
        -webkit-flex: 1;
        flex: 1;
        box-sizing: border-box;
        padding-top: 6px;
        padding-left: 6px;
        padding-right: 6px;
        height: 28px;
        background-color: #eee;
        border-radius: 14px;
    }
    
    header .input input {
        width: 100%;
        height: 20px;
        vertical-align: top;
        font-size: 14px;
    }
    
    header .button {
        box-sizing: border-box;
        width: 80px;
        height: 28px;
        margin-left: 8px;
        line-height: 26px;
        text-align: center;
        font-size: 16px;
        color: #444;
        border-radius: 14px;
        border: 1px solid #aaa;
    }
    
    section {
        width: 100%;
        height: auto;
    }
    
    .option {
        width: 100%;
        height: 60px;
        margin-bottom: 2px;
        background-color: #fff;
    }
    
    .option .icon {
        position: absolute;
        top: 20px;
        left: 16px;
        width: 16px;
        height: 20px;
        background-image: url(../image/icon_address_none.png);
        background-size: 16px 20px;
        background-repeat: no-repeat;
        background-position: center center;
    }
    
    .option .info {
        box-sizing: border-box;
        width: 100%;
        height: 60px;
        padding-left: 40px;
        padding-right: 16px;
    }
    
    .option .info .top {
        box-sizing: border-box;
        width: 100%;
        height: 35px;
        padding-top: 15px;
        padding-bottom: 4px;
        font-size: 16px;
        text-align: left;
        color: #444;
    }
    
    .option .info .bottom {
        box-sizing: border-box;
        width: 100%;
        height: 25px;
        padding-bottom: 11px;
        font-size: 12px;
        text-align: left;
        color: #888;
    }
    
    .highlight {
        opacity: 0.7;
    }
    </style>
</head>

<body>
    <header>
        <div class="input">
            <input id="searchText" type="search" placeholder="小区/写字楼">
        </div>
        <div class="button">清空</div>
    </header>
    <section id="list">
        <!--         <div class="option">
            <div class="icon"></div>
            <div class="info">
                <div class="top">门头沟</div>
                <div class="bottom">正在获取...</div>
            </div>
        </div> -->
    </section>
</body>
<script type="text/template" id="template">
    {{~it.results:value}}
    <div class="option" tapmode onclick="fnSelectAddress('{{=value.address}}')">
        <div class="icon"></div>
        <div class="info">
            <div class="top">{{=value.name}}</div>
            <div class="bottom">{{=value.address}}</div>
        </div>
    </div>
    {{~}}
</script>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript">
apiready = function() {
    fnInitUIInput();
};

function fnInitUIInput() {
    var searchText = $api.byId('searchText');
    var rect = $api.offset(searchText);
    var UIInput = api.require('UIInput');
    UIInput.open({
        rect: {
            x: rect.l,
            y: rect.t,
            w: rect.w,
            h: rect.h
        },
        styles: {
            bgColor: '#eee',
            size: 14,
            color: '#000',
            placeholder: {
                color: '#ccc'
            }
        },
        autoFocus: false,
        maxRows: 1,
        placeholder: '请输入地址',
        keyboardType: 'default',
        fixedOn: api.frameName
    }, function(ret, err) {
        if (ret) {
            var uiInputId = ret.id;
            if (ret && ret.eventType == "show") {
                setTimeout(function() {
                    UIInput.popupKeyboard({id: uiInputId});
                }, 300)
            } else if (ret.eventType == "change") {
                UIInput.value({
                    id: uiInputId
                }, function(ret, err) {
                    if (ret) {
                        if (ret.status) {
                            //console.log(ret.msg);
                            fnSearchInCity(ret.msg);
                        }
                    } else {
                        alert(JSON.stringify(err));
                    }
                });
            }
        } else {
            alert(JSON.stringify(err));
        }
    });
}

function fnSearchInCity(msg_) {
    var currentCity = $api.getStorage('currentCity');
    var bMap = api.require('bMap');
    bMap.searchInCity({
        city: currentCity.name,
        keyword: msg_,
        pageIndex: 0,
        pageCapacity: 20
    }, function(ret) {
        if (ret.status) {
            fnUpdateSearchList(ret);
        }
    });
}

function fnUpdateSearchList(data_) {
    var template = $api.byId('template');
    var list = $api.byId('list');
    var tempFn = doT.template(template.innerHTML);
    var html = tempFn(data_);
    $api.html(list, html);
}

function fnSelectAddress(address_) {
    api.execScript({
        name: 'setaddress',
        frameName: 'setaddress_frame',
        script: 'setAddressDetail(\'' + address_ + '\');'
    });
    api.closeWin();
}
</script>

</html>
